package com.cq.hd.common.utils;

import com.github.binarywang.utils.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;

/**
 * @description: 二维码生成和读的工具类
 * @author: mYunYu
 * @date: 2021/7/12 10:14
 * @version: v1.0
 */
public class QrCodeCreateUtil {

    /**
     * @Description: 生成包含字符串信息的二维码图片
     * @Param: [content：二维码携带信息, qrCodeSize：二维码图片大小]
     * @Return: byte[]
     * @Auther: mYunYu
     * @Date: 2021/7/12 10:14
     */
    public static byte[] createQrCode(String content, int qrCodeSize) throws WriterException, IOException{
        //设置二维码纠错级别ＭＡＰ
        Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  // 矫错级别
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //创建比特矩阵(位矩阵)的QR码编码的字符串
        BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
        // 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)
        int matrixWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
        // 使用比特矩阵画并保存图像
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < matrixWidth; i++){
            for (int j = 0; j < matrixWidth; j++){
                if (byteMatrix.get(i, j)){
                    graphics.fillRect(i-100, j-100, 1, 1);
                }
            }
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", outputStream);

        return outputStream.toByteArray();

    }

    /**
     * @Description: 读二维码并输出携带的信息
     * @Param: [data]
     * @Return: java.lang.String
     * @Auther: mYunYu
     * @Date: 2021/7/12 10:15
     */
    public static String readQrCode(byte[] data) throws IOException{
        //从输入流中获取字符串信息
        ByteArrayInputStream in = new ByteArrayInputStream(data);    //将b作为输入流；
        BufferedImage image = ImageIO.read(in);
        //将图像转换为二进制位图源
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        Result result = null ;
        try {
            result = reader.decode(bitmap);
        } catch (ReaderException e) {
            e.printStackTrace();
        }
        return result.getText();
    }

    public static void main(String[] args) throws IOException, WriterException {
        byte[] qrCode = createQrCode("119", 900);
        System.out.println(readQrCode(qrCode));
    }

}
